<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * wap登录页控制器
 * @package  htdocs
 * @subpackage  shop
 * create by allen.qiang
 * @version 1.0
 */
require_once('core/shop.php');
quanli_require('framework/wx/wxHelp.php');

if(isset($_REQUEST['ajaxFlg']))
{
    global $G_SHOP;

    //-----------登陆绑定-----------//
    if($_REQUEST['ajaxFlg']== 1){
        if (!isset($_REQUEST['mobile']) && empty($_REQUEST['mobile'])) {
            returnJsonData('请输入手机号');
        }
        if (!is_mobile($_REQUEST['mobile'])) {
            returnJsonData('请输入正确的手机号');
        }
        if (!isset($_REQUEST['mobileCode']) && empty($_REQUEST['mobileCode'])) {
            returnJsonData('请输入短信验证码');
        }
        if (!isset($_REQUEST['yzmCode']) && empty($_REQUEST['yzmCode'])) {
            returnJsonData('请输入动态验证码');
        }

        $yzmCode = $_REQUEST['yzmCode'];
        $mobile = $_REQUEST['mobile'];
        $mobileCode = $_REQUEST['mobileCode'];

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $mobile);
        if (is_null($sms_ysm) || $sms_ysm != md5($mobileCode)) {
            returnJsonData('短信验证码有误', 400);
        }

        //检查动态验证码
        $yzm = $G_SHOP->sessionCache_get('yzm');
        if (is_null($yzm) || $yzm != md5(strtolower($yzmCode))) {
            returnJsonData('动态验证码有误', 400);
        }

        $param = array('mobile' => $mobile, 'useFlg' => 1);

        //查询手机号
        $result = null;
        $companyMaster = new companyMaster();
        $temp_user = $companyMaster->search($param);
        if ($temp_user) {
            $temp_user = $temp_user[0];
            $result = $companyMaster->bindOpenId($temp_user['company_id'], wxhelp::getOpenId());
        } else {
            $sysDis = new sysDistributionTechnician();
            $temp_user = $sysDis->search($param);
            if ($temp_user) {
                $temp_user = $temp_user[0];
                $result = $sysDis->bindOpenId($temp_user['distribution_technician_id'], wxhelp::getOpenId());
            }else{
                global $G_SHOP;
                $user_master = new sysUser($G_SHOP->DBCA);//实例用户类
                $temp_user = $user_master->searchUserInfo($param);
                if ($temp_user) {
                    $temp_user = $temp_user[0];
                    $result = $user_master->bindOpenId($temp_user['uuid'], wxhelp::getOpenId());
                }
            }
        }

        if($result && $result['code'] == 200){
            returnJsonData('绑定成功', 200);
        }
        returnJsonData('绑定失败');
    }
    //-----------发短信-----------//
    else if($_REQUEST['ajaxFlg'] == 2){
        if(!isset($_REQUEST['mobile']) && empty($_REQUEST['mobile'])) {
            returnJsonData('请输入手机号');
        }
        if(!is_mobile($_REQUEST['mobile'])) {
            returnJsonData('请输入正确的手机号');
        }

        $template = 'get_pwd';          //消息模板
        $mobile = $_REQUEST['mobile'];  //手机号码
        $G_SHOP->sessionCache_remove('sms_yzm_' . $mobile);
        $G_SHOP->sessionCache_remove('sms_yzm_time_' . $mobile);

        $current_time = time(); //当前时间戳
        $last_send_time = $G_SHOP->sessionCache_get('sms_yzm_time_' . $mobile); //上一次发放短信的时间戳
        if (!empty($last_send_time) && $current_time - $last_send_time < 60) {
            returnJsonData('手机短信已发送，请一分钟以后再试');
        }
        quanli_include('module/base/common/quanliSms.class.php');
        $quanliSms = new quanliSms();
        $sms_yzm = $quanliSms->createSmsCode();
        $result = WxHelp::sendMessage($mobile, array('$sms_yzm'=>$sms_yzm), 'get_pwd');

        if ($result == 200) {
            //保存短信验证码到redis
            $G_SHOP->sessionCache_set('sms_yzm_' . $mobile, md5($sms_yzm), 300);//将验证码信息保存到memcache中去
            $G_SHOP->sessionCache_set('sms_yzm_time_' . $mobile, time(), 300);
            returnJsonData('发送成功', 200);
        }
        returnJsonData('发送失败');
    }
    //-----------注銷-----------//
    else if($_REQUEST['ajaxFlg'] == 3){
        $G_SHOP->sessionCache_remove('WX_USERINFO');
        Header("Location: /wap/login.html");
    }
}

$user = wxhelp::getUser();
if($user){
    Header("Location: /wap/order.html");
}

$G_SHOP->smarty->display ('login.tpl' );